package com.putong35.common.web.ui;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.putong35.common.baseactfrag.fragment.BaseFragment;
import com.putong35.common.databinding.FragmentHtmlContentWebBinding;

/**
 * Simple to Introduction
 *
 * @Author: ${USER}
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: ${DATE} ${TIME}
 * @Version: 1.0.0
 */
public class HtmlContentWebFragment extends BaseFragment {

    private String htmlContent;

    public HtmlContentWebFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param htmlContent Parameter 1.
     * @return A new instance of fragment CourseSummaryFragment.
     */
    public static HtmlContentWebFragment newInstance(String htmlContent) {
        HtmlContentWebFragment fragment = new HtmlContentWebFragment();
        Bundle args = new Bundle();
        args.putString("htmlContent", htmlContent);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            htmlContent = getArguments().getString("htmlContent");
        }
    }

    FragmentHtmlContentWebBinding fragmentHtmlContentWebBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        updateView();
        return view;
    }


    private void updateView() {
        Log.e("pww", "onCreateView: getDetail  " + htmlContent);
        if (!TextUtils.isEmpty(htmlContent)) {
            CharSequence formatHtl = Html.fromHtml(htmlContent);
            fragmentHtmlContentWebBinding.webView.getSettings().setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
//        webView.loadData(data, "text/html", "UTF-8");//API提供的标准用法，无法解决乱码问题
            Log.e("pww", "onCreateView: formatHtl.toString() " + htmlContent);
//            webView.loadData(formatHtl.toString(), "text/html; charset=UTF-8", null);//这种写法可以正确解码
            fragmentHtmlContentWebBinding.webView.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);

//            .loadDataWithBaseURL("file://", html,mimeType, encoding, "about:blank");
        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        fragmentHtmlContentWebBinding = FragmentHtmlContentWebBinding.inflate(inflater);
        return fragmentHtmlContentWebBinding.getRoot();
    }

    public void loadData(String htmlContent) {
        this.htmlContent = htmlContent;
        updateView();
    }


}
